<?php
namespace WebAdmin\Controller;
use WebAdmin\Controller\BaseController;
class LinkController extends BaseController {

	//ThinkPHP的构造函数
	public function _initialize() {
		//自动加载的类
		parent::_initialize();
		new \Org\Admin_autoload();
	}

    public function index(){
    	$this->assign('page_title','友情连结');
    	$NewsModel = D("Link");
    	$items = $NewsModel->select();
    	$this->assign('volist',$items);
    	
    	//$this->assign('')
        $this->display();
    }
    
    public function add(){
    	if(!empty($_POST)) {
    		$NewsModel = D("Link");
    		$_POST['is_show'] = 1;
    		if(!empty($_FILES['imgfile']['name'])){
			import ( '@.ORG.UploadFile' );
			$upload = new \UploadFile ();
			// 设置文件大小
			$upload->maxSize = 3292200;
			// 设置文件保存规则唯一
			$upload->saveRule = 'uniqid';
			// 设置上传文件的格式
			$upload->allowExts = array (
					'jpg',
					'png',
					'jpeg',
					'gif' 
			);
			// 保存路径
			$root_path = dirname(dirname(dirname(dirname(__FILE__))));
    		$upload->savePath = $root_path.'/www/Public/Uploads/';
			// 设置需要生成缩略图，仅对图像文件有效
			$upload->thumb = false;
			// 删除原图
			$upload->thumbRemoveOrigin = false;
			// 上传失败返回错误信息
			if (! $upload->upload ()) {
				$this->error ( $upload->getErrorMsg () );
			} else {
				// $this->success('上传成功');
				// 获取上传文件的信息
				$inf = $upload->getUploadFileInfo ();
			}
    	 }
			if(!empty($inf)){
				$inf[0]['savepath'] = str_replace($root_path."/www","",$inf[0]['savepath']);
				$_POST['imgurl'] = str_replace("./","/",$inf[0]['savepath']) . $inf [0] ['savename'];//"http://".FRONT_SITE_NAME.
			}
				
				$data = $NewsModel->create ( $_POST );
				$insert_id = $NewsModel->add ( $data );
				if ($insert_id) {
					
					header ( "Content-type: text/html; charset=utf-8" );
					echo "<script>window.location.href='" . __APP__ . "/index.php/Link/';</script>";
					exit ();
				}
			
		} else {
			$this->display ();
		}
    }
    
    public function getData(){
    	$DataModel = D("Link");
    	$condition = array();
    	if(!empty($_GET['id'])){
    		$condition[] = "id=".(int)$_GET['id'];
    	}
    	if(!empty($_GET['title'])){
    		$condition[] = "title LIKE '%".$_GET['title']."%'";
    	}
    	if(!empty($_GET['url'])){
    		$condition[] = "url LIKE '%".$_GET['title']."%'";
    	}
    	if(!empty($_GET['sort'])){
    		$condition[] = 'sort = '.(int)$_GET['sort'];
    	}
    	$conditions = implode(" AND ",$condition);
    	$items = array();
    	$page = (int)$_GET['page'];
    	$items['page'] = $page>0 ? $page :1;
    	$items['records'] = $DataModel->count();
    	$limit = $_GET['rows'] ? (int)$_GET['rows']: 20;
    	$items['total'] = ceil($items['records']/$limit);
    	$list = $DataModel->where($conditions)->page($page)->limit($limit)->select();
    	$items['rows'] = $list;
    	$this->ajaxReturn($items);
    }
    
    
    public function edit(){
    	$DataModel = D("Link");
    	if(!empty($_POST)) {
    		if(!empty($_FILES['imgfile']['name'])){
    		import ( '@.ORG.UploadFile' );
    		$upload = new \UploadFile ();
    		// 设置文件大小
    		$upload->maxSize = 3292200;
    		// 设置文件保存规则唯一
    		$upload->saveRule = 'uniqid';
    		// 设置上传文件的格式
    		$upload->allowExts = array (
    				'jpg',
    				'png',
    				'jpeg',
    				'gif'
    		);
    		// 保存路径
    		$root_path = dirname(dirname(dirname(dirname(__FILE__))));
    		$upload->savePath = $root_path.'/www/Public/Uploads/';
    		// 设置需要生成缩略图，仅对图像文件有效
    		$upload->thumb = false;
    		// 设置需要生成缩略图的文件前缀
    		$upload->thumbPrefix = 'm_'; // 生产缩略图也可以根据需要生成1张或多张，2张：'m_,s_'
    		// 设置缩略图最大宽度
    		$upload->thumbMaxWidth = '150'; // 2张的不同设置：'150,200'
    		// 设置缩略图最大高度
    		$upload->thumbMaxHeight = '200';
    		// 删除原图
    		$upload->thumbRemoveOrigin = true;
    		// 上传失败返回错误信息
    		if (! $upload->upload ()) {
    			$this->error ( $upload->getErrorMsg () );
    		} else {
    			// $this->success('上传成功');
    			// 获取上传文件的信息
    			$inf = $upload->getUploadFileInfo ();
    		}
    		
    		}
    	    if(!empty($inf)){
    				//unlink old file
    				$item = $DataModel->where("id=".(int)$_POST['id'])->select();
    				unlink($root_path."/www".str_replace("http://".FRONT_SITE_NAME,"",$item[0]['imgurl']));
    				$inf[0]['savepath'] = str_replace($root_path."/www","",$inf[0]['savepath']);
    				 
    				$_POST['imgurl'] = str_replace("./","/",$inf[0]['savepath']) . $inf [0] ['savename'];//"http://".FRONT_SITE_NAME.
    			}
    		
    		$DataModel->where("id=".(int)$_POST['id'])->save($_POST);
    		
    		header("Content-type: text/html; charset=utf-8");
    		echo "<script>window.location.href='".__APP__."/index.php/Link/';</script>";exit;
    	 
    	}else{
    		$item = $DataModel->where("id=".(int)$_GET['id'])->select();
    		$this->assign('vo',$item[0]);
    		$this->display();
    	}
    }
    
    public function delete(){
    	if(!empty($_POST)) {
    		$DataModel = D("Link");
    		$id = $_POST['id'];
    		$result = $DataModel->delete((int)$id);
    		
    		if($result){
    			header("Content-type: text/html; charset=utf-8");
    			echo "<script>layer.closeAll(); \$table.trigger(\"reloadGrid\");</script>";exit;
    		}
    	}
    }
       
    
}